﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UserControl_DateType : System.Web.UI.UserControl
{
    /// <summary>
    /// 方法：初始化页面
    /// </summary>
    public void InitMethod(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                //初始化时间筛选模式
                this.DDLDateType.Items.Add("日报");
                this.DDLDateType.Items.Add("周报");
                this.DDLDateType.Items.Add("月报");
                
                //初始化时间筛选事件
                this.DDLDateType_SelectedIndexChanged(sender, e);

                //初始化“开始年份”下拉列表控件
                BindYearDDL(this.DDLStartYear_M);
                //初始化“开始月份”下拉列表控件
                DDLStartYear_M_SelectedIndexChanged(sender, e);
                //初始化“结束年份”下拉列表控件
                BindYearDDL(this.DDLEndYear_M);
                //初始化“结束月份”下拉列表控件
                DDLEndYear_M_SelectedIndexChanged(sender, e);

                //初始化“开始年份”下拉列表控件
                BindYearDDL(this.DDLStartYear_W);
                //初始化“开始周”下拉列表控件
                DDLStartYear_W_SelectedIndexChanged(sender, e);
                //初始化“结束年份”下拉列表控件
                BindYearDDL(this.DDLEndYear_W);
                //初始化“结束周”下拉列表控件
                DDLEndYear_W_SelectedIndexChanged(sender, e);
            }
        }
        catch { }
    }
    /// <summary>
    /// 绑定年份下拉列表
    /// </summary>
    /// <param name="dropDownListControl">DropDownList</param>
    private void BindYearDDL(DropDownList dropDownListControl)
    {
        int year = DateTime.Now.Year;   //当前年份 
        dropDownListControl.Items.Clear();
        //
        for (int i = 0; i < 3; i++)     //初始化3个年份
        {
            dropDownListControl.Items.Add(new ListItem((year - i).ToString()));
        }
    }

    /// <summary>
    /// 只读属性：查询模式(1:自然月；2:445月)
    /// </summary>
    public Object QueryType
    {
        get
        {
            if ("自然月".Equals(this.DDLQueryType.SelectedItem.Text))
                return "1";
            else
                return "2";
        }
    }
    /// <summary>
    /// 只读属性：时间模式（1:月报；2:周报；3:日报）
    /// </summary>
    public Object DateType
    {
        get { return Cache["DateType"]; }
    }
    /// <summary>
    /// 只读属性：开始年份
    ///     （仅当时间模式选择为日报时无值）
    /// </summary>
    public Object StartYear
    {
        get
        {
            if ((string)DateType == "1")
                return this.DDLStartYear_M.SelectedItem.Text;
            else if ((string)DateType == "2")
                return this.DDLStartYear_W.SelectedItem.Text;
            return null;
        }
    }
    /// <summary>
    /// 只读属性：结束年份
    ///     （仅当时间模式选择为日报时无值）
    /// </summary>
    public Object EndYear
    {
        get
        {
            if ((string)DateType == "1")
                return this.DDLEndYear_M.SelectedItem.Text;
            else if ((string)DateType == "2")
                return this.DDLEndYear_W.SelectedItem.Text;
            return null;
        }
    }
    /// <summary>
    /// 只读属性：开始月份或周
    ///     （仅当时间模式选择为日报时无值）
    /// </summary>
    public Object StartMonthOrWeek
    {
        get
        {
            if ((string)DateType == "1")
                return this.DDLStartMonth.SelectedItem.Text;
            else if ((string)DateType == "2")
                return this.DDLStartWeek.SelectedItem.Text;
            return null;
        }
    }
    /// <summary>
    /// 只读属性：结束月份或周
    ///     （仅当时间模式选择为日报时无值）
    /// </summary>
    public Object EndMonthOrWeek
    {
        get
        {
            if ((string)DateType == "1")
                return this.DDLEndMonth.SelectedItem.Text;
            else if ((string)DateType == "2")
                return this.DDLEndWeek.SelectedItem.Text;
            return null;
        }
    }
    /// <summary>
    /// 只读属性：开始日期
    ///     （仅当时间模式选择为日报时有值）
    /// </summary>
    public Object StartDate
    {
        get
        {
            if ((string)DateType == "3")
            {
                return this.txtStartDate.Text;
            }
            else if ((string)QueryType == "1" && (string)DateType == "2")
            {
                DateTime date = DateTime.Parse((string)StartYear + "-1-1");
                //表示的日期是星期几
                int dayOfWeek = ((int)date.DayOfWeek) == 0 ? 7 : (int)date.DayOfWeek;
                if (int.Parse((string)StartMonthOrWeek) == 1)
                {
                    return date.ToShortDateString();
                }
                else
                {
                    return date.AddDays((int.Parse((string)StartMonthOrWeek) - 1) * 7 - dayOfWeek + 1).ToShortDateString();
                }                
            }
            return null;
        }
    }
    /// <summary>
    /// 只读属性：结束日期
    ///     （仅当时间模式选择为日报时有值）
    /// </summary>
    public Object EndDate
    {
        get
        {
            if ((string)DateType == "3")
                return this.txtEndDate.Text;
            else if ((string)QueryType == "1" && (string)DateType == "2")
            {
                DateTime date = DateTime.Parse((string)EndYear + "-1-1");
                //表示的日期是星期几
                int dayOfWeek = ((int)date.DayOfWeek) == 0 ? 7 : (int)date.DayOfWeek;
                return date.AddDays((int.Parse((string)EndMonthOrWeek) - 1) * 7 + (7 - dayOfWeek)).ToShortDateString();
            }
            return null;
        }
    }

    /// <summary>
    /// 时间模式选择
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DDLDateType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if ("月报".Equals(DDLDateType.SelectedItem.Text))
        {
            this.divMonth.Visible = true;
            this.divDay.Visible = false;
            this.divWeek.Visible = false;
            //时间模式
            Cache["DateType"] = "1";
        }
        else if ("周报".Equals(DDLDateType.SelectedItem.Text))
        {
            this.divMonth.Visible = false;
            this.divDay.Visible = false;
            this.divWeek.Visible = true;
            //时间模式
            Cache["DateType"] = "2";
        }
        else if ("日报".Equals(DDLDateType.SelectedItem.Text))
        {
            this.divMonth.Visible = false;
            this.divDay.Visible = true;
            this.txtStartDate.Text=DateTime.Now.ToShortDateString();
            this.txtEndDate.Text = DateTime.Now.ToShortDateString();
            this.divWeek.Visible = false;
            //时间模式
            Cache["DateType"] = "3";
        }
    }
    /// <summary>
    /// 选择开始年份（月）
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DDLStartYear_M_SelectedIndexChanged(object sender, EventArgs e)
    {
        int month = DateTime.Now.Month; //当前月份
        //
        string startYear = this.DDLStartYear_M.SelectedItem.Text;
        if (DateTime.Now.Year.ToString() != startYear)
            month = 12;
        this.DDLStartMonth.Items.Clear();
        for (int i = 0; i < month; i++)
        {
            this.DDLStartMonth.Items.Add(new ListItem((month - i).ToString()));
        }
    }
    /// <summary>
    /// 选择结束年份（月）
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DDLEndYear_M_SelectedIndexChanged(object sender, EventArgs e)
    {
        int month = DateTime.Now.Month; //当前月份
        //
        string endYear = this.DDLEndYear_M.SelectedItem.Text;
        if (DateTime.Now.Year.ToString() != endYear)
            month = 12;
        this.DDLEndMonth.Items.Clear();
        for (int i = 0; i < month; i++)
        {
            this.DDLEndMonth.Items.Add(new ListItem((month - i).ToString()));
        }
    }
    /// <summary>
    /// 选择开始年份(周)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DDLStartYear_W_SelectedIndexChanged(object sender, EventArgs e)
    {
        //默认将日期设置为当天
        DateTime date = DateTime.Now;
        string startYear = this.DDLStartYear_W.SelectedItem.Text;
        if (date.Year.ToString() != startYear)
        {
            //如果选择的不是当年，则将日期定为选择年份的最后一天
            date = DateTime.Parse(startYear + "-12-31");
        }
        //表示的日期是该年中的第几天。
        int dayOfYear = date.DayOfYear;
        //表示的日期是星期几
        int dayOfWeek = ((int)date.DayOfWeek) == 0 ? 7 : (int)date.DayOfWeek;
        //
        dayOfYear = dayOfYear - dayOfWeek;
        //当前第几周
        int week;
        //如果选择了按照自然月查看
        if ((string)QueryType == "1")
            week = dayOfYear % 7 != 0 ? dayOfYear / 7 + 2 : dayOfYear / 7 + 1;
        else
            week = dayOfYear / 7 + 1;   //445月从第一个星期一算第一周
        //
        this.DDLStartWeek.Items.Clear();
        for (int i = 0; i < week; i++)
        {
            this.DDLStartWeek.Items.Add(new ListItem((week - i).ToString()));
        }
    }
    /// <summary>
    /// 选择结束年份(周)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DDLEndYear_W_SelectedIndexChanged(object sender, EventArgs e)
    {
        //默认将日期设置为当天
        DateTime date = DateTime.Now;
        string endYear = this.DDLEndYear_W.SelectedItem.Text;
        if (date.Year.ToString() != endYear)
        {
            //如果选择的不是当年，则将日期定为选择年份的最后一天
            date = DateTime.Parse(endYear + "-12-31");
        }
        //表示的日期是该年中的第几天。
        int dayOfYear = date.DayOfYear;
        //表示的日期是星期几
        int dayOfWeek = ((int)date.DayOfWeek) == 0 ? 7 : (int)date.DayOfWeek;
        //
        dayOfYear = dayOfYear - dayOfWeek;
        //当前第几周
        int week;
        //如果选择了按照自然月查看
        if ((string)QueryType == "1")
            week = dayOfYear % 7 != 0 ? dayOfYear / 7 + 2 : dayOfYear / 7 + 1;
        else
            week = dayOfYear / 7 + 1;   //445月从第一个星期一算第一周
        this.DDLEndWeek.Items.Clear();
        for (int i = 0; i < week; i++)
        {
            this.DDLEndWeek.Items.Add(new ListItem((week - i).ToString()));
        }
    }
}
